<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
          :model="where"
          label-width="77px"
          class="ele-form-search"
          @keyup.enter.native="reload"
          @submit.native.prevent>
        <el-row :gutter="15">

          <el-col :lg="6" :md="12">
            <el-form-item label="门禁名称:">
              <el-input
                  clearable
                  v-model="where.title"
                  placeholder="请输入门禁名称"/>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                  type="primary"
                  icon="el-icon-search"
                  class="ele-btn-icon"
                  @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
          ref="table"
          :where="where"
          :datasource="url"
          :columns="columns"
          :selection.sync="selection"
          height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
              @click="openEdit(null)"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              size="small"
              v-if="permission.includes('sys:index:edit')">添加
          </el-button>
          <el-button
              @click="removeBatch"
              type="danger"
              icon="el-icon-delete"
              class="ele-btn-icon"
              size="small"
              v-if="permission.includes('sys:index:dall')">删除
          </el-button>
        </template>
        <!-- 用户名列 -->


        <!-- 角色列 -->
        <template slot="roles" slot-scope="{row}">
          <el-tag
              v-for="item in row.roles"
              :key="item.id"
              size="mini"
              type="primary"
              :disable-transitions="true">
            {{ item.name }}
          </el-tag>
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-switch
              v-model="row.status"
              @change="editStatus(row)"
              :active-value="1"
              :inactive-value="2"/>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-popconfirm
              class="ele-action"
              title="临时开启门禁(限制90分钟)"
              @confirm="Open(row)">
            <el-link
                type="danger"
                slot="reference"
                :underline="false"
                icon="el-icon-edit"
            >开门
            </el-link>
          </el-popconfirm>



          <el-popconfirm
            class="ele-action"
            title="确定要断电吗？"
            @confirm="Openend(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-edit"
            >断电
            </el-link>
          </el-popconfirm>
          <el-link
              type="primary"
              :underline="false"
              icon="el-icon-edit"
              @click="openEdit(row)"
              v-if="permission.includes('sys:index:edit')">修改
          </el-link>

          <el-popconfirm
            class="ele-action"
            style="margin-left: 50px;"
          title="确定要删除此门禁吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
               style="color: black;"
            >删除
            </el-link>
          </el-popconfirm>

        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <doorlist-edit
        :data="current"
        :visible.sync="showEdit"
        @done="reload"/>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import DoorlistEdit from './door-list-edit';

export default {
  name: 'SystemUser',
  components: {DoorlistEdit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/door/index',
      // 表格列配置
      columns: [
        // {
        //   columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',

        // },
        // {
        //   prop: 'id',
        //   label: 'ID',
        //   width: 60,
        //   align: 'center',
        //   showOverflowTooltip: true,

        // },

        {
          prop: 'name',
          label: '设备名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 80,
        },
        {
          prop: 'pid_name',
          label: '上级门禁',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 50,
        },
        {
          prop: 'door_id',
          label: '设备id',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 70,
        },


        // {
        //   prop: 'create_time',
        //   label: '创建时间',
        //   align: 'center',
        //   showOverflowTooltip: true,
        //   minWidth: 160,
        //   formatter: (row, column, cellValue) => {
        //     return this.$util.toDateString(cellValue);
        //   }
        // },
        {
          columnKey: 'action',
          label: '操作',
          minWidth: 300,
          align: 'center',
          resizable: false,
          slot: 'action',

        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {

      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
console.log('1111')
      const loading = this.$loading({lock: true});
      this.$http.post('/door/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据')
        return;
      }
      this.$confirm('确定要删除选中的门禁数据吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/door/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({type: 'success', message: res.data.msg});
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status
      })
      this.$http.post('/door/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({type: 'success', message: res.data.msg});
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    Open(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/door/open', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    Openend(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/door/openend', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style scoped>
</style>
